package com.ssmTest.service.impl;

import sun.misc.BASE64Encoder;

import java.io.*;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MsgSent {

  public static String EncodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    //ȷ�����㷽��
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    //��ȡ����
    md5.update(str.getBytes());
    //����APIҪ���ȡ32λMD5����
    String newstr = new BigInteger(1, md5.digest()).toString(16);
    newstr = newstr.toUpperCase();
    return newstr;
  }

  public static String EncodeByBase64(String str) throws UnsupportedEncodingException {

    BASE64Encoder base64en = new BASE64Encoder();
    String newstr = base64en.encode(str.getBytes());
    return newstr;
  }


  public static String sendPost(String[] param) {


    //��ȡʱ���
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//�������ڸ�ʽ
    String date = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ�䣬Ҳ��ʹ�õ�ǰʱ���
    //��������ֱ�Ϊ�˻�ID Account Sid ��  Auth Token
    String s = "eb4771d415662c74a72b9435acca7f28" + "5681826276e5952171865afcae60a043" + date;
    try {
      s = MsgSent.EncodeByMd5(s);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    String url = "";
    url = "https://api.ucpaas.com/2014-06-30/Accounts/eb4771d415662c74a72b9435acca7f28/Messages/templateSMS?sig=" + s;


    //����Authorization��Ϣ
    s = "eb4771d415662c74a72b9435acca7f28" + ":" + date;
    try {
      s = MsgSent.EncodeByBase64(s);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    //���÷��͵�����


    String sjson = "{" +
        " \"templateSMS\" : {" +
        "    \"appId\"       : \"f51c42c6bacc404f96b832a36a2fd59a\"," +
        "    \"param\"       : " + "\"" + param[2] + "\"" + "," +     //�ҵ�ģ�������������������
        "    \"templateId\"  : " + "\"" + param[0] + "\"" + "," +
        "    \"to\"          : " + "\"" + param[1] + "\"" +
        "    }" +
        "}";


    System.out.println(sjson);


    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
      URL realUrl = new URL(url);
      // �򿪺�URL֮�������
      URLConnection conn = realUrl.openConnection();
      // ����ͨ�õ���������
      conn.setRequestProperty("host", "api.ucpaas.com");
      conn.setRequestProperty("accept", "application/json");
      conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
      conn.setRequestProperty("Authorization", s);


      // ����POST�������������������
      conn.setDoOutput(true);
      conn.setDoInput(true);
      out = new PrintWriter(conn.getOutputStream());
      out.print(sjson);
      out.flush();
      in = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      System.out.println("���� POST ��������쳣��" + e);
      e.printStackTrace();
    } finally {
      try {
        if (out != null) {
          out.close();
        }
        if (in != null) {
          in.close();
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    return result;
  }

  public static String sendmessage(String phone) {
    String[] pa = new String[3];
    pa[0] = "262780";
    pa[1] = phone;
    pa[2] = Integer.toString((int) (Math.random() * 100000));
    //Account Sid
    String sr = MsgSent.sendPost(pa);
    if (!sr.equals(""))
      return pa[2];
    else
      return "";
  }

  public static void main(String[] args) {
    System.out.println(
        sendmessage(""));
  }

}
